package com.yupi.springbootinit.statemachine.rule.config;

import com.yupi.springbootinit.statemachine.enums.OrderStateEnum;
import com.yupi.springbootinit.statemachine.enums.ProcessInstanceStateEnum;
import com.yupi.springbootinit.statemachine.rule.RuleEngine;
import com.yupi.springbootinit.statemachine.rule.impl.OrderBusinessRules;
import com.yupi.springbootinit.statemachine.rule.impl.ProcessInstanceBusinessRules;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * 规则引擎配置
 * 在应用启动时注册所有业务规则
 */
@Slf4j
@Component
public class RuleEngineConfig implements CommandLineRunner {
    
    @Autowired
    private RuleEngine ruleEngine;
    
    @Override
    public void run(String... args) throws Exception {
        log.info("开始注册业务规则...");
        
        // 注册订单相关业务规则
        registerOrderRules();
        
        // 注册工序实例相关业务规则
        registerProcessInstanceRules();
        
        log.info("业务规则注册完成，规则统计: {}", ruleEngine.getRuleStatistics());
    }
    
    /**
     * 注册订单相关业务规则
     */
    private void registerOrderRules() {
        String entityType = "ORDER";
        
        // 订单创建规则
        ruleEngine.registerRule(
            entityType,
            OrderStateEnum.CREATED,
            OrderStateEnum.CREATED,
            new OrderBusinessRules.OrderCreationRule()
        );
        
        // 工艺发布规则
        ruleEngine.registerRule(
            entityType,
            OrderStateEnum.CREATED,
            OrderStateEnum.ROUTING_PUBLISHED,
            new OrderBusinessRules.ProcessSheetPublishRule()
        );
        
        // 生产排期规则
        ruleEngine.registerRule(
            entityType,
            OrderStateEnum.ROUTING_PUBLISHED,
            OrderStateEnum.SCHEDULE_PUBLISHED,
            new OrderBusinessRules.ProductionScheduleRule()
        );
        
        // 订单完成规则
        ruleEngine.registerRule(
            entityType,
            OrderStateEnum.INSPECTING,
            OrderStateEnum.COMPLETED,
            new OrderBusinessRules.OrderCompletionRule()
        );
        
        log.info("订单业务规则注册完成");
    }
    
    /**
     * 注册工序实例相关业务规则
     */
    private void registerProcessInstanceRules() {
        String entityType = "PROCESS_INSTANCE";
        
        // 工序开始规则
        ruleEngine.registerRule(
            entityType,
            ProcessInstanceStateEnum.PENDING,
            ProcessInstanceStateEnum.IN_PROGRESS,
            new ProcessInstanceBusinessRules.ProcessStartRule()
        );
        
        // 工序完成规则
        ruleEngine.registerRule(
            entityType,
            ProcessInstanceStateEnum.IN_PROGRESS,
            ProcessInstanceStateEnum.PENDING_INSPECTION,
            new ProcessInstanceBusinessRules.ProcessCompletionRule()
        );
        
        ruleEngine.registerRule(
            entityType,
            ProcessInstanceStateEnum.IN_PROGRESS,
            ProcessInstanceStateEnum.COMPLETED,
            new ProcessInstanceBusinessRules.ProcessCompletionRule()
        );
        
        // 外协工序规则
        ruleEngine.registerRule(
            entityType,
            ProcessInstanceStateEnum.PENDING,
            ProcessInstanceStateEnum.IN_PROGRESS,
            new ProcessInstanceBusinessRules.OutsourcingProcessRule()
        );
        
        // 返工规则
        ruleEngine.registerRule(
            entityType,
            ProcessInstanceStateEnum.REJECTED,
            ProcessInstanceStateEnum.IN_PROGRESS,
            new ProcessInstanceBusinessRules.ReworkRule()
        );
        
        log.info("工序实例业务规则注册完成");
    }
}